<?php
class User extends CI_Controller {
    public $tbl = '';

    public function __construct() {
        parent::__construct();

        // used in the view
        $this->load->helper('url');
        $this->load->model('Mmuser');
        $this->load->library('twig');
        $this->lang->load('form'); // load language with file form_lang.php and named _lang.php extension
    }

    public function admin() {
        $data['listUser'] = $this->Mmuser->getUser();

        $this->twig->display('user/listUser.html.twig', $data);
    }

    public function updateUser($id='') {
        $data['userById'] = $id ? $this->Mmuser->getUser($id) : array();

//        echo "<pre>"; print_r($data); echo "</pre>"; die;

        $data['form'] = $this->show_form('formUser', $data['userById']);


        unset($data['userById']['password']);
        if ($this->form->valid) {
            // get the validated and xss cleaned (TRUE) post data as an array
            $post = $this->form->get_post(TRUE);
            $this->Mmuser->processForm($post, $id);

            // redirect to success page
            redirect('user/admin');
        } else {
            // write the errors to the $data array
            // NOTE: this must be done after $form->get()
            $data['errors'] = $this->form->errors;

            // the following variable can be used to get all errors in an associative array
            // might be useful when used with json_encode and AJAX functions:

            // $this->form->error_array;
        }

        $this->twig->display('user/ucUser.html.twig', $data);
    }

    public function delUser($id) {
        if ($id != 1) {
            $this->Mmuser->delUser($id);
            redirect('user/admin');
        } else {
            throw new Exception('You are not allowed');
            exit();
        }
    }

    function show_form($form_name,$edit= array()) {
        /* Form */
        $this->load->library('form');
        $this->form->clear();
        $this->form
            ->open(current_url(), $form_name,'enctype="multipart/form-data"')
            ->html("<div id='tab_1'>")

            ->html("<div class='form_line'>")
            ->text($this->tbl.'username', $this->lang->line($this->tbl."username"), 'required|max_length[32]',isset($edit[$this->tbl.'username']) ? $edit[$this->tbl.'username'] : "" )

            ->html("</div><div class='form_line '>")
            ->password($this->tbl.'password', $this->lang->line("password"), 'required')
            ->html("</div>");
        if (!empty($edit)) {
            $this->form->html("</div><div class='form_line '>")
                ->select($this->tbl.'status', array(1 => 'Active', 2 => 'Inactive'), $this->lang->line("status"),
                    'required')
                ->html("</div>");
        }
        $this->form->html("</div>");


        $this->form

            ->html("<center>")
            ->html("<div class='form_msg'></div>")
            ->html("<div class='form_line'>")
            ->submit('Save','sub',array("class"=>""))
            ->html("</div>")
            //->html("<div id='formLoading'><img src='".base_url()."templates/images/loader.gif'></div>")
            ->html("</center>")

            ->html("</div>");
        return $this->form->get();
    }
}
